<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<meta HTTP-EQUIV="Expires" CONTENT="0">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>产品绑定属性查询</title>
<style type="text/css">
	@import "../dojo-release-1.5.0/dijit/themes/tundra/tundra.css";
	@import "../dojo-release-1.5.0/dojo/resources/dojo.css";
	@import "../dojo-release-1.5.0/dojox/grid/resources/tundraGrid.css";
</style>
<script type="text/javascript" src="../dojo-release-1.5.0/dojo/dojo.js" djConfig="parseOnLoad:true"></script>
<script type="text/javascript" src="../framework/uiutil.js"></script>
<script type="text/javascript">

dojo.require("dojo.io.iframe");

dojo.addOnLoad(function(){
	initProductSelect();
	dojo.connect(dojo.byId("productNameSelect"),"onblur",initCodeNameSelect);
});


//初始化产品选择列表
function initProductSelect(){
	dojo.xhrGet({
		 url:"../bkgrd/getProductList.do",
		 handleAs:"json",
		 load:function(response){
			 ps = [];
			 for(var i in response){
				 ps.push({value:response[i],name:response[i]});
			 }
			 D.initCombo("productNameSelect",ps)
		 }
	});
}


//初始化绑定码名称列表
function initCodeNameSelect(){
	var productName = D.getComboValue("productNameSelect");
	dojo.xhrPost({
		 url:"../bkgrd/getAllBindingCodeNameList.do",
		 preventCache:true,
		 content:{productName:productName},
		 handleAs:"json",
		 load:function(response){
			 ps = [];
			 for(var i in response){
				 ps.push({value:response[i],name:response[i]});
			 }
			 D.initCombo("conditionCodeNameSelect",ps)
			 D.initMultiSelect("bindingCodeNameList",ps)
             if(response.length===0){
                 alert("此产品无属性可用");
             }
		 }
	});
}

function query(){
	var productName = D.getComboValue("productNameSelect");
	var conditionCodeName = D.getComboValue("conditionCodeNameSelect");
	var startCode = dojo.byId("startCode").value;
	var endCode = dojo.byId("endCode").value;
	var bindingCodeNameList = D.getMultiSelectValue("bindingCodeNameList");

	if(productName == ''){
		alert("请选择产品类型");
		return;
	}
	if(bindingCodeNameList.length>9){
		alert("请选择小于10项的绑定属性输出");
		return;
	}
	bindingCodeNameListStr="";

	var columnWidth = 100/(bindingCodeNameList.length+2);
	
	var layout = [{'name': conditionCodeName , 'field': 'queryCode',width:columnWidth+'%'},{'name':'标识码','field':'idCode',width:columnWidth+'%'}];
	for(var i=0;i<bindingCodeNameList.length;i++){
		layout.push({'name':bindingCodeNameList[i],'field':'code_'+i,width:columnWidth+'%'});
		bindingCodeNameListStr += (bindingCodeNameList[i]+",")
	}
	var grid = dijit.byId("grid");
	grid.setStructure([layout]);
	
	dojo.xhrPost({
		 url:"../bkgrd/queryBindingCodeCollection.do",
		 encoding : 'UTF-8',
		 preventCache:true,
		 content:{
			 productName:productName,
			 conditionCodeName:conditionCodeName,
			 startCode:startCode,
			 endCode:endCode,
			 bindingCodeNameListStr:bindingCodeNameListStr
		 },
		 handleAs:"json",
		 load:function(response){
			if(response == ""){
				alert("没有结果，请检查查询条件。");
			}
			displayResult(response);
		 }
	});
}

function lost() {
    var grid = dijit.byId("grid");
    var item = D.getSelectedRowOfTable(grid)[0];
    if(item == null){
        alert("请至少选择表格中的一行");
    }else{
        var productName = D.getComboValue("productNameSelect");
        var idCode=item.idCode[0];
        var queryStr="?productName="+productName+"&"+"idCode="+idCode;
        dojo.xhrPut({
            url:"../bkgrd/unbinding.do"+queryStr,
            handleAs:"json",
            preventCache:true,
            load:function(response){
                if (response==0){
                    alert("已丢弃");
                    query()
                } else if(response==1){
                    alert("服务器异常")
                }else{
                    alert("未知错误")
                }
            }
        });
    }
}

function exportExcel(){
	var productName = D.getComboValue("productNameSelect");
	var conditionCodeName = D.getComboValue("conditionCodeNameSelect");
	var startCode = dojo.byId("startCode").value;
	var endCode = dojo.byId("endCode").value;
	var bindingCodeNameList = D.getMultiSelectValue("bindingCodeNameList");
	bindingCodeNameListStr="";
	for(var i=0;i<bindingCodeNameList.length;i++){
		bindingCodeNameListStr += (bindingCodeNameList[i]+",")
	}

	dijit.byId("exportExcel").setDisabled(true);
	dijit.byId("exportExcel").setLabel('正在生成报表...'); 
	
	dojo.xhrPost({
		 url:"../bkgrd/exportBindingCodeCollection.do",
		 encoding : 'UTF-8',
		 preventCache:true,
		 content:{
			 productName:productName,
			 conditionCodeName:conditionCodeName,
			 startCode:startCode,
			 endCode:endCode,
			 bindingCodeNameListStr:bindingCodeNameListStr
		 },
		 handleAs:"text",
		 load:function(response){
			dijit.byId("exportExcel").setLabel('直接导出Excel'); 
			dijit.byId("exportExcel").setDisabled(false);
			if(response == ""){
				alert("记录导出失败");
				return;
			}
			downloadFile(response);
		 },
		 error:function(error){
			 alert(error);
			 dijit.byId("exportExcel").setLabel('直接导出Excel');
			 dijit.byId("exportExcel").setDisabled(false);
		 }
	});
}

function exportExcelOfMany(){
    var productName = D.getComboValue("productNameSelect");
    var conditionCodeName = D.getComboValue("conditionCodeNameSelect");
    var startCode = dojo.byId("startCode").value;
    var endCode = dojo.byId("endCode").value;
    var bindingCodeNameList = D.getMultiSelectValue("bindingCodeNameList");
    bindingCodeNameListStr="";
    for(var i=0;i<bindingCodeNameList.length;i++){
        bindingCodeNameListStr += (bindingCodeNameList[i]+",")
    }

    dijit.byId("exportExcelOfMany").setDisabled(true);
    dijit.byId("exportExcelOfMany").setLabel('正在生成报表...');

    dojo.xhrPost({
        url:"../bkgrd/exportBindingCodeCollectionOfMany.do",
        encoding : 'UTF-8',
        preventCache:true,
        content:{
            productName:productName,
            conditionCodeName:conditionCodeName,
            startCode:startCode,
            endCode:endCode,
            bindingCodeNameListStr:bindingCodeNameListStr
        },
        handleAs:"text",
        load:function(response){
            dijit.byId("exportExcelOfMany").setLabel('导出多个Excel');
            dijit.byId("exportExcelOfMany").setDisabled(false);
            if(response == ""){
                alert("记录导出失败");
                return;
            }
            response=JSON.parse(response);
            console.log(response);
            for (var i = 0; i <response.length ; i++) {
                console.log(response[i]);
                downloadFileOfMany(response[i],i);
            }
        },
        error:function(error){
            alert(error);
            dijit.byId("exportExcelOfMany").setLabel('导出多个Excel');
            dijit.byId("exportExcelOfMany").setDisabled(false);
        }
    });
}

function downloadFile(filename){
	var exportIframeName = "downloadPdfIframe";   
    var iframe = dojo.io.iframe.create(exportIframeName);  
    var url = "../bkgrd/downloadFile.do?fileName="+filename;   
    dojo.io.iframe.setSrc(iframe, url, true);
}

function downloadFileOfMany(filename,id){
    var exportIframeName = "downloadPdfIframe"+id;
    var iframe = dojo.io.iframe.create(exportIframeName);
    var url = "../bkgrd/downloadFile.do?fileName="+filename;
    dojo.io.iframe.setSrc(iframe, url, true);
}


function displayResult(resultList){
    D.fillTableData("grid",resultList);
}

//详细报告
function retrospect(){
	var item = D.getSelectedRowOfTable("grid")[0];
	if(item == null){
		alert("请至少选择表格中的一行");
	}
	var productName = D.getComboValue("productNameSelect");
	//window.location.href = "testResultRetrospect.html?productName="+productName+"&idCode="+item["idCode"];
	window.open("testResultRetrospect.html?productName="+productName+"&idCode="+item["idCode"]);
}
</script>

<style type="text/css">
#grid{
	border:1px solid #333;
	width:90%;
	margin:10px;
	height:500px;
	font-size:0.9em;
}
</style>
</head>
<body class="tundra">

<table width="90%">
<tr><td>请选择产品类型：</td><td><select dojoType="dijit.form.ComboBox" id="productNameSelect" value="" required="true" ></select></td>
<td>查询绑定码类型：</td><td><select dojoType="dijit.form.ComboBox" id="conditionCodeNameSelect"></select></td></tr>

<tr><td>查询范围</td><td>
<input type="text" dojoType="dijit.form.TextBox" id="startCode" size="30" trim="true" value=""/>
<br/>&nbsp;&nbsp;至&nbsp;&nbsp;<br/>
<input type="text" dojoType="dijit.form.TextBox" id="endCode" size="30" trim="true" value=""/>
</td><td>输出绑定属性</td><td>
<select id="bindingCodeNameList" dojoType="dijit.form.MultiSelect" style="width:184px;height:50px;"></select></td>
</tr>


<tr><td colspan="4" align="center">
<button dojoType="dijit.form.Button" id="query" onclick="query()">查询绑定属性</button>
<button dojoType="dijit.form.Button" id="lost" onclick="lost()">丢失绑定属性</button>
<button dojoType="dijit.form.Button" id="exportExcel" onclick="exportExcel()">直接导出Excel</button>
    <button dojoType="dijit.form.Button" id="exportExcelOfMany" onclick="exportExcelOfMany()">导出多个Excel</button>
<button dojoType="dijit.form.Button" id="retrospect" onclick="retrospect()">详细检测报告</button>
</td></tr>
</table>

<div id="gridDiv">
<table id="grid" dojoType="dojox.grid.DataGrid"></table>
</div>
</body>
</html>